<?php

class IndexController extends Controller_Base
{
    public function init()
    {
        parent::init();
        $roles = Module_User_Role::getInstance()->getListAll();
        $arrRole = array();
        foreach ($roles as $key => $value) {
            $arrRole[$value['id']] = $value;
        }
        $this->arrRole = $arrRole;
        SmartyAdapter::assign('arrRole', $arrRole);
    }

    public function indexAction()
    {
        $ts = strtotime(date("Y-m-d", time()));
        $expire_time = $ts + (86400 * 7);
        //统计数量
        $where = $this->getWhere();
        $count = Module_Mediate_Case::getInstance()->getAllBySearch(
            [
                "sum(CASE WHEN addTime > {$ts} THEN 1 ELSE 0 END) addCount",
                "sum(CASE WHEN caseStatus = 'case_mediate' THEN 1 ELSE 0 END) mediateCount",
                "sum(CASE WHEN repayStatus in ('wait_repay','break_repay','keep_repay','finish_repay') THEN 1 ELSE 0 END) repaymentCount",
                "sum(CASE WHEN caseStatus = 'close_case' THEN 1 ELSE 0 END) closeCount",
                "sum(CASE WHEN entrustExpireTime < {$expire_time} and entrustExpireTime > 0 THEN 1 ELSE 0 END) expireeCount",
                "sum(CASE WHEN caseStatus = 'case_mediate' and mediateStatus = 'first_mediate' THEN 1 ELSE 0 END) firstMediateCount",
                "sum(CASE WHEN caseStatus = 'case_mediate' and mediateStatus in ('in_mediate','reject_mediate') THEN 1 ELSE 0 END) inMediateCount",
                "sum(CASE WHEN caseStatus = 'case_mediate' and mediateStatus = 'finish_mediate' THEN 1 ELSE 0 END) finishMediateCount",
                "sum(CASE WHEN repayStatus in ('wait_repay','keep_repay') THEN 1 ELSE 0 END) inRepaymentCount",
                "sum(CASE WHEN repayStatus = 'break_repay' THEN 1 ELSE 0 END) breakRepaymentCount",
                "sum(CASE WHEN repayStatus = 'finish_repay' THEN 1 ELSE 0 END) finishRepaymentCount",
            ],
            [
                'where' =>array_merge($where,['sysStatus=1'])
            ],true);
        SmartyAdapter::assign('count', $count[0]);

        $callOutQuery = [
            'callNotConnected' => 0, //未接通数
            'callConnected' => 0, //接通数
            'callConnectedRate' => 0, //接通率
            'talkTime' => 0, //通话时长
        ];
        //呼入
        $callinQuery = [
            'callNotConnected' => 0, //未接通数
            'callConnected' => 0, //接通数
            'callConnectedRate' => 0, //接通率
            'talkTime' => 0, //通话时长
        ];
        $calloutlist = Module_Mediate_CallRecord::getInstance()->getAllBySearch([], ['where'=>array_merge($where,["addTime>={$ts}"])]);
        if (!empty($calloutlist)) {
            foreach ($calloutlist as $k => $v) {
                if(in_array($v['callType'],[2,3])){
                    //接通
                    if ($v['talkType'] == 1) {
                        $callinQuery['callConnected'] += 1;

                    } elseif ($v['talkType'] == 2) {
                        $callinQuery['callNotConnected'] += 1;
                    }
                    $callinQuery['talkTime'] +=  $v['talkTime'];
                }elseif($v['callType'] == 1){
                    if ($v['talkType'] == 1) {
                        $callOutQuery['callConnected'] += 1;
                    } elseif ($v['talkType'] == 2) {
                        $callOutQuery['callNotConnected'] += 1;

                    }
                    $callOutQuery['talkTime'] +=  $v['talkTime'];
                }
            }
        }
        if ($callOutQuery['callConnected'] > 0 || $callOutQuery['callNotConnected'] > 0) {
            $callOutQuery['callConnectedRate'] = sprintf("%.4f", $callOutQuery['callConnected'] / ($callOutQuery['callConnected'] + $callOutQuery['callNotConnected']));
            $callOutQuery['callConnectedRate'] *= 100;
        }
        if( $callOutQuery['talkTime'] ){
            $callOutQuery['talkTime']  = gmdate("H:i:s",$callOutQuery['talkTime']);
        }
        SmartyAdapter::assign('callOutQuery', $callOutQuery);


        if ($callinQuery['callConnected'] > 0 || $callinQuery['callNotConnected'] > 0) {
            $callinQuery['callConnectedRate'] = sprintf("%.4f", $callinQuery['callConnected'] / ($callinQuery['callConnected'] + $callinQuery['callNotConnected']));
            $callinQuery['callConnectedRate'] *= 100;
        }
        if( $callinQuery['talkTime'] ){
            $callinQuery['talkTime']  = gmdate("H:i:s",$callinQuery['talkTime']);
        }
        SmartyAdapter::assign('callinQuery', $callinQuery);

        //今日还款计划
        $returnlist = Module_Mediate_ReturnedMoneyPlanDetail::getInstance()->getAllBySearch([], ['where'=>array_merge($where,["repaymentDate = {$ts} and isDel=1"])]);
        SmartyAdapter::assign('returnlist', $returnlist);
        SmartyAdapter::assign('planStatusArr', Module_Mediate_Case::$planStatusArr);

        //今日联系计划
        $ts_end = $ts + 86400;
        $contactlist = Module_Mediate_Case::getInstance()->getAllBySearch(['id','litigantName','customLabel','nextTime','todayContactStutus'], ['where'=>array_merge($where,["nextTime > 0 and nextTime < {$ts_end} and sysStatus=1"]),'order'=>"nextTime"]);
        foreach ($contactlist as $key => $val){
            $contactlist[$key]['customLabel'] = Module_Mediate_Case::$customLabel[$val['customLabel']];
            $contactlist[$key]['nextTime'] =  str_replace(array('AM','PM',),array('上午','下午'),date('d日 A h:i', $val['nextTime']));
            $contactlist[$key]['todayContactStutus'] = Module_Mediate_Case::$todayContactStutus[$val['todayContactStutus']];

        }
        $password = Module_User_Administrator::encodePassword("PWD123456", $this->admin['register_time']);
        SmartyAdapter::assign('contactlist', $contactlist);
        SmartyAdapter::assign('isalert', $this->admin['password'] == $password);

    }

    public function testAction()
    {

    }

    public function uploadAction()
    {
//        phpinfo();die;
    }
    public function selectworkeridAction(){
        $request = $this->getRequest();
        $workerId = $request->getPost('workerId', '');
        if($workerId){
            Lib_Redis_String::set($this->admin['id'],$workerId);
            AjaxTool::returnJsonData(200);
        }
        AjaxTool::returnJsonData(4005);
    }
}
